package com.kk.crowd.service.impl;

import com.kk.crowd.entity.po.AddressPO;
import com.kk.crowd.entity.po.AddressPOExample;
import com.kk.crowd.entity.po.OrderPO;
import com.kk.crowd.entity.po.OrderProjectPO;
import com.kk.crowd.entity.vo.AddressVO;
import com.kk.crowd.entity.vo.OrderProjectVO;
import com.kk.crowd.entity.vo.OrderVO;
import com.kk.crowd.mapper.AddressPOMapper;
import com.kk.crowd.mapper.OrderPOMapper;
import com.kk.crowd.mapper.OrderProjectPOMapper;
import com.kk.crowd.service.api.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderProjectPOMapper orderProjectPOMapper;

    @Autowired
    private OrderPOMapper orderPOMapper;

    @Autowired
    private AddressPOMapper addressPOMapper;

    public OrderProjectVO getOrderProjectVO(Integer projectId, Integer returnId) {

        return orderProjectPOMapper.getOrderProjectVO(returnId);
    }

    public List<AddressVO> getAddressVORemote(Integer memberId) {
        AddressPOExample addressPOExample = new AddressPOExample ( );
        addressPOExample.createCriteria ().andMemberIdEqualTo (memberId);

        List<AddressPO> addressPOList = addressPOMapper.selectByExample (addressPOExample);
        List<AddressVO> addressVOList = new ArrayList<AddressVO> ( );

        for (AddressPO addressPO : addressPOList){
            AddressVO addressVO = new AddressVO ( );
            BeanUtils.copyProperties (addressPO,addressVO);
            addressVOList.add (addressVO);
        }

        return addressVOList;
    }

    // 对方法取消只读，设置事务传播行为  readOnly = false(默认是false)
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class, readOnly = false)
    public void saveAddress(AddressVO addressVO) {
        AddressPO addressPO = new AddressPO ( );

        BeanUtils.copyProperties (addressVO,addressPO);

        addressPOMapper.insert (addressPO);
    }

    // 对方法取消只读，设置事务传播行为  readOnly = false(默认是false)
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class, readOnly = false)
    public void saveOrderRemote(OrderVO orderVO) {
        OrderPO orderPO = new OrderPO ( );

        BeanUtils.copyProperties (orderVO,orderPO);

        OrderProjectPO orderProjectPO = new OrderProjectPO ( );

        BeanUtils.copyProperties (orderVO.getOrderProjectVO (),orderProjectPO);

        // 保存orderPO 自动生成的主键 是 orderProjectPO 需要用到的外键
        // useGeneratedKeys="true" keyProperty="id"
        orderPOMapper.insert (orderPO);

        // 从orderPO 中获取 orderId
        Integer orderId = orderPO.getId ( );

        // 将 orderId 设置到 orderProjectPO 中
        orderProjectPO.setOrderId (orderId);

        orderProjectPOMapper.insert (orderProjectPO);
    }
}
